home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / util / Str.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  240 lines

  1. /*    $Header: /usr/people/sam/fax/util/RCS/Str.h,v 1.7 1994/02/28 14:23:59 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #ifndef _Str_
  26. #define    _Str_
  27.  
  28. #include "stdlib.h"
  29. #include "Obj.h"
  30.  
  31. // Temporary strings are generated by the concatenation operators.
  32. // They are designed to avoid calls to malloc, and as such are not
  33. // meant to be used directly.
  34. class fxStr;
  35.  
  36. class fxTempStr {
  37. public:
  38.     fxTempStr(fxTempStr const &other);
  39.     ~fxTempStr();
  40.  
  41.     // C++ uses these operators to perform the first concatenation
  42.     friend fxTempStr operator|(fxStr const&, fxStr const&);
  43.     friend fxTempStr operator|(fxStr const&, char const*);
  44.     friend fxTempStr operator|(char const*, fxStr const&);
  45.  
  46.     // Susequent concatenations use these operators.  These operators
  47.     // just append the argument data to the temporary, avoiding malloc
  48.     // when possible.
  49.     // XXX these aren't really const, although they are declared as such
  50.     // XXX to avoid warnings. In a land of very advanced compilers, this
  51.     // XXX strategy may backfire.
  52.     friend fxTempStr& operator|(const fxTempStr&, fxStr const& b);
  53.     friend fxTempStr& operator|(const fxTempStr&, char const* b);
  54.  
  55.     operator char*() const;
  56.     operator int() const;
  57.     operator float() const;
  58.     operator double() const;
  59.  
  60.     u_int length() const;
  61. protected:
  62.     char    indata[100];        // inline data, avoiding malloc
  63.     char*    data;            // points at indata or heap
  64.     u_int    slength;        // same rules as fxStr::slength
  65.  
  66.     friend class fxStr;
  67.  
  68.     fxTempStr(char const *, u_int, char const *, u_int);
  69.     fxTempStr& concat(char const* b, u_int bl);
  70. };
  71.  
  72. inline fxTempStr::operator char*() const    { return data; }
  73. inline fxTempStr::operator int() const        { return atoi(data); }
  74. inline fxTempStr::operator float() const    { return float(atof(data)); }
  75. inline fxTempStr::operator double() const    { return double(atof(data)); }
  76. inline u_int fxTempStr::length() const        { return slength - 1; }
  77.  
  78. //----------------------------------------------------------------------
  79.  
  80. class fxStr {
  81.     friend class fxTempStr;
  82. public:
  83.     fxStr(u_int l=0);
  84.     fxStr(char const *s);
  85.     fxStr(char const *s, u_int len);
  86.     fxStr(fxStr const&);
  87.     fxStr(int, char const* format);
  88.     fxStr(long, char const* format);
  89.     fxStr(float, char const* format);
  90.     fxStr(double, char const* format);
  91.     fxStr(const fxTempStr&);
  92.     ~fxStr();
  93.  
  94.     /////////////////////////////////////////////////////
  95.     u_long hash() const;
  96.  
  97.     operator char*() const
  98.     { return data; }
  99.     operator int() const
  100.     { return atoi(data); }
  101.     operator float() const
  102.     { return float(atof(data)); }
  103.     operator double() const
  104.     { return double(atof(data)); }
  105.  
  106.     u_int length() const { return slength-1; }
  107.  
  108.     char& operator[](u_int i) const
  109.     {   fxAssert(i<slength-1,"Invalid Str[] index");
  110.     return data[i]; }
  111.  
  112.     void operator=(const fxTempStr& s);
  113.     void operator=(fxStr const& s);
  114.     void operator=(char const *s);
  115.  
  116.     /////////////////////////////////////////////////////
  117.     // Comparison
  118.     friend fxBool operator==(fxStr const&, fxStr const&);
  119.     friend fxBool operator==(fxStr const&, char const*);
  120.     friend fxBool operator==(char const*, fxStr const&);
  121.  
  122.     friend fxBool operator!=(fxStr const&, fxStr const&);
  123.     friend fxBool operator!=(fxStr const&, char const*);
  124.     friend fxBool operator!=(char const*, fxStr const&);
  125.  
  126.     friend fxBool operator>=(fxStr const&, fxStr const&);
  127.     friend fxBool operator>=(fxStr const&, char const*);
  128.     friend fxBool operator>=(char const*, fxStr const&);
  129.  
  130.     friend fxBool operator<=(fxStr const&, fxStr const&);
  131.     friend fxBool operator<=(fxStr const&, char const*);
  132.     friend fxBool operator<=(char const*, fxStr const&);
  133.  
  134.     friend fxBool operator>(fxStr const&, fxStr const&);
  135.     friend fxBool operator>(fxStr const&, char const*);
  136.     friend fxBool operator>(char const*, fxStr const&);
  137.  
  138.     friend fxBool operator<(fxStr const&, fxStr const&);
  139.     friend fxBool operator<(fxStr const&, char const*);
  140.     friend fxBool operator<(char const*, fxStr const&);
  141.  
  142.     int compare(fxStr const *a) const { return ::compare(*this, *a); }
  143.     friend int compare(fxStr const&, fxStr const&);
  144.     friend int compare(fxStr const&, char const*);
  145.     friend int compare(char const*, fxStr const&);
  146.  
  147.     /////////////////////////////////////////////////////
  148.     // Concatenation
  149.     friend fxTempStr& operator|(const fxTempStr&, fxStr const&);
  150.     friend fxTempStr& operator|(const fxTempStr&, char const*);
  151.  
  152.     friend fxTempStr operator|(fxStr const&, fxStr const&);
  153.     friend fxTempStr operator|(fxStr const&, char const*);
  154.     friend fxTempStr operator|(char const*, fxStr const&);
  155.  
  156.     /////////////////////////////////////////////////////
  157.     // Misc
  158.     fxStr copy() const;
  159.     fxStr extract(u_int start,u_int len) const;
  160.     fxStr cut(u_int start,u_int len);
  161.     fxStr head(u_int) const;
  162.     fxStr tail(u_int) const;
  163.     void lowercase();
  164.     void raisecase();
  165.  
  166.     void remove(u_int posn,u_int len=1);
  167.  
  168.     void resize(u_int len, fxBool reallocate = FALSE);
  169.     void setMaxLength(u_int maxlen);
  170.  
  171.     void append(char a);
  172.     void append(char const *s, u_int len=0);
  173.     void append(const fxTempStr& s)
  174.     { append((char*)s, s.slength-1); }
  175.     void append(fxStr const& s)
  176.     { append((char*)s, s.slength-1); }
  177.  
  178.     void insert(char a, u_int posn=0);
  179.     void insert(char const *, u_int posn=0, u_int len=0);
  180.     void insert(const fxTempStr& s, u_int posn=0)
  181.     { insert((char*)s, posn, s.slength-1); }
  182.     void insert(fxStr const& s, u_int posn=0)
  183.     { insert((char*)s, posn, s.slength-1); }
  184.  
  185.     /////////////////////////////////////////////////////
  186.     // Parsing
  187.     u_int next(u_int posn, char delimiter) const;
  188.     u_int next(u_int posn, char const *delimiters, u_int len=0) const;
  189.     u_int next(u_int posn, fxStr const& delimiters) const
  190.     { return next(posn, (char*)delimiters, delimiters.slength-1); }
  191.                 
  192.     u_int nextR(u_int posn, char delimiter) const;
  193.     u_int nextR(u_int posn, char const*, u_int len=0) const;
  194.     u_int nextR(u_int posn, fxStr const& delimiters) const
  195.     { return nextR(posn, (char*)delimiters, delimiters.slength-1); }
  196.                       
  197.     u_int skip(u_int posn, char a) const; 
  198.     u_int skip(u_int posn, char const *, u_int len=0) const;
  199.     u_int skip(u_int posn, fxStr const& delimiters) const
  200.     { return skip(posn, (char*)delimiters, delimiters.slength-1); }
  201.  
  202.     u_int skipR(u_int posn, char a) const;
  203.     u_int skipR(u_int posn, char const *, u_int len=0) const;
  204.     u_int skipR(u_int posn, fxStr const& delimiters) const
  205.     { return skipR(posn, (char*)delimiters, delimiters.slength-1); }
  206.  
  207.     fxStr token(u_int & posn, char delimiter) const;
  208.     fxStr token(u_int & posn, char const * delimiters,
  209.     u_int delimiters_len = 0) const;
  210.     fxStr token(u_int & posn, fxStr const & delimiters) const
  211.     { return token(posn, delimiters.data, delimiters.slength-1); }
  212.  
  213.     fxStr tokenR(u_int & posn, char delimiter) const;
  214.     fxStr tokenR(u_int & posn, char const * delimiters,
  215.     u_int delimiters_len = 0) const;
  216.     fxStr tokenR(u_int & posn, fxStr const & delimiters) const
  217.     { return tokenR(posn, delimiters.data, delimiters.slength-1); }
  218.  
  219. protected:
  220.     // slength is one greater than the true length of the data.
  221.     // This is because the data is null-terminated. However, the
  222.     // data may contain nulls; they will be ignored. This is to
  223.     // provide compatibility with ordinary C-style strings, and
  224.     // with arbitrary data. slength is always positive.
  225.     u_int slength;
  226.  
  227.     // data points to the actual data. It is always a valid pointer.
  228.     char * data; 
  229.  
  230.     // zero-length string support
  231.     // resizeInternal doesn't update slength or handle null termination
  232.     static char emptyString;
  233.     void fxStr::resizeInternal(u_int);
  234.  
  235.     int findEndBuffer(const char *, u_int buflen) const;
  236.     int findBuffer(const char *buf, u_int buflen) const;
  237.     void bracketBuffer(const char *, u_int buflen, int &, int &) const;
  238. };
  239. #endif /* _Str_ */
  240.